package com.smart.stock.module.maindata.controller;

import com.smart.stock.common.util.stock.StockUtil;
import com.smart.stock.module.maindata.entity.StockPriceDaily;
import com.smart.stock.module.maindata.services.StockDailyIndexesService;
import com.smart.stock.module.maindata.services.StockDailyPriceServices;
import com.smart.stock.module.spider.util.TradingDateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * add by andrew
 **/
@Api(tags = "个股行情信息")
@Slf4j
@Controller
@RequestMapping("/StockDailyPrice")
public class StockDailyPriceController {
    @Autowired
    private StockDailyPriceServices stockDailyPriceServices;
    @Autowired
    private TradingDateUtil tradingDateUtil;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private StockDailyIndexesService stockDailyIndexesService;

    @ApiOperation(value = "stockDaily")
    @RequestMapping("/{code}")
    public String stockDaily(@PathVariable String code, Model model) throws Exception {
        log.info("tradingDateUtil...");
        String startDate= tradingDateUtil.minusDateNow(1,0,0,"yyyyMMdd");
        String end=tradingDateUtil.getDateYYYYMMdd();
        List<StockPriceDaily> list ;
        StringBuilder data=new StringBuilder();

        log.info("getDailyLine...");
        //获取股票历史数据
        if (stockDailyPriceServices.getStockDailyRows(code)==0){
            stockDailyPriceServices.refreshDailyPrice(code,startDate,end);
        }
        list= stockDailyPriceServices.getDailyLine(code);

        log.info("getStockVolIncrease...");
        list = StockUtil.getStockVolIncrease(list);
        list = StockUtil.getStockProfitLoss(list);
        list = StockUtil.getStockShapes(list);
        list = StockUtil.getStockNewHight(list);//新高数据计算较慢，每天的价格都需要与前面的数据比较

        log.info("data.append...");
        for (StockPriceDaily row :list) {
            data.append("[")
                    .append("'").append(row.getTradeDate()).append("'").append(",")
                    .append(row.getVol()).append(",")
                    .append(row.getVolIncrease()).append(",")
                    //新高
                    .append("'").append(row.getIsNewHigh()).append("'").append(",")
                    //形态
                    .append("'").append(row.getIsShapeOfHammer()).append("'").append(",")
                    .append("'").append(row.getIsShapeOfHangOneself()).append("'").append(",")
                    .append("'").append(row.getIsShapeOfSwallowUp()).append("'").append(",")
                    .append("'").append(row.getIsShapeOfSwallowDown()).append("'").append(",")
                    .append("'").append(row.getIsShapeOfStabUp()).append("'").append(",")
                    .append("'").append(row.getIsShapeOfStabDown()).append("'").append(",")

                    //开盘收盘涨幅
                    .append(row.getOpen()).append(",")
                    .append(row.getClose()).append(",")
                    .append(row.getLow()).append(",")
                    .append(row.getHigh()).append(",")
                    .append(row.getPctchg()).append(",")
                    .append(row.getAmount())
                    .append("]").append(",")
            ;
        }
        //拼接数据，返回到页面
        data.deleteCharAt(data.length()-1);
//        model.addAttribute("pagetitle",code);
        model.addAttribute("code",code);
        model.addAttribute("data",data.toString());

        //最高点、最低点、最低点左侧次低点vo1、最低点右侧次低点vo2，传给前端划线
        log.info("StockUtil.getStockPriceMinMax...");
        List minmax = StockUtil.getStockPriceMinMax(template, code);
        StockPriceDaily maxVo = (StockPriceDaily)minmax.get(0);
        StockPriceDaily minVo = (StockPriceDaily)minmax.get(1);

        model.addAttribute("max","['"+maxVo.getTradeDate()+"',"+maxVo.getClose()+"]");
        model.addAttribute("min","['"+minVo.getTradeDate()+"',"+minVo.getClose()+"]");

        if (minmax.size()>2){
            StockPriceDaily vo1 = (StockPriceDaily)minmax.get(2);
            StockPriceDaily vo2 = (StockPriceDaily)minmax.get(3);
            model.addAttribute("vo1","['"+vo1.getTradeDate()+"',"+vo1.getClose()+"]");
            model.addAttribute("vo2","['"+vo2.getTradeDate()+"',"+vo2.getClose()+"]");
        }else {
            model.addAttribute("vo1","['"+maxVo.getTradeDate()+"',"+maxVo.getClose()+"]");
            model.addAttribute("vo2","['"+minVo.getTradeDate()+"',"+minVo.getClose()+"]");
        }

//        model.addAttribute("days",minmax.get(3));
//        model.addAttribute("diffs",minmax.get(4));
        model.addAttribute("days",1);
        model.addAttribute("diffs",1);

        String shapeOfCurrent = StockUtil.getShapeOfCurrent(list);
        model.addAttribute("shape","'"+shapeOfCurrent+"'");

        //当前价画线
        StockPriceDaily pointOfRight = StockUtil.getPointOfRight(list);
        StockPriceDaily pointOfLeft = StockUtil.getPointOfLeft(list);
        Double close = pointOfRight.getClose();
        Double open = pointOfRight.getOpen();
        model.addAttribute("open",open);
        model.addAttribute("close","'"+close+"'");
        model.addAttribute("currentEnd","['"+pointOfRight.getTradeDate()+"',"+pointOfRight.getClose()+"]");
        model.addAttribute("currentBegin","['"+pointOfLeft.getTradeDate()+"',"+pointOfRight.getClose()+"]");

        boolean isIndexes = StockUtil.isIndexes(code);
        model.addAttribute("isIndexes",String.valueOf(isIndexes));


        //先定义需要输出的属性
        String stockName = "";
        String maybeProfitLoss = "[]";

        //个股名称
        ArrayList<String> stockInfo = StockUtil.getStockInfoByCode(template, code);
        if (stockInfo.size()>0) {
            stockName = stockInfo.get(0);
        }

        //盈亏比
        log.info("StockUtil.getMaybeProfitLoss...");
        maybeProfitLoss = StockUtil.getMaybeProfitLoss(list).toString();
        model.addAttribute("maybeProfitLoss",maybeProfitLoss);

        //股票名称，传给前端，写在最后
        model.addAttribute("stockCode","'"+code+"'");
        model.addAttribute("stockName",stockName);
        return "/stock/info/stock_daily";
    }


}
